package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.domain.AppUserLogin;
import com.ruoyi.system.mapper.AppUserLoginMapper;
import com.ruoyi.system.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppRedPacketSendMapper;
import com.ruoyi.system.domain.AppRedPacketSend;
import com.ruoyi.system.service.IAppRedPacketSendService;

/**
 * 红包发送记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class AppRedPacketSendServiceImpl extends ServiceImpl<AppRedPacketSendMapper, AppRedPacketSend> implements IAppRedPacketSendService {
    @Autowired
    private AppRedPacketSendMapper appRedPacketSendMapper;

    /**
     * 查询红包发送记录
     *
     * @param id 红包发送记录主键
     * @return 红包发送记录
     */
    @Override
    public AppRedPacketSend selectAppRedPacketSendById(Long id) {
        return appRedPacketSendMapper.selectAppRedPacketSendById(id);
    }

    /**
     * 查询红包发送记录列表
     *
     * @param appRedPacketSend 红包发送记录
     * @return 红包发送记录
     */
    @Override
    public List<AppRedPacketSend> selectAppRedPacketSendList(AppRedPacketSend appRedPacketSend) {
        return appRedPacketSendMapper.selectAppRedPacketSendList(appRedPacketSend);
    }

    /**
     * 新增红包发送记录
     *
     * @param appRedPacketSend 红包发送记录
     * @return 结果
     */
    @Override
    public int insertAppRedPacketSend(AppRedPacketSend appRedPacketSend) {
        appRedPacketSend.setCreateTime(DateUtils.getNowDate());
        return appRedPacketSendMapper.insertAppRedPacketSend(appRedPacketSend);
    }

    /**
     * 修改红包发送记录
     *
     * @param appRedPacketSend 红包发送记录
     * @return 结果
     */
    @Override
    public int updateAppRedPacketSend(AppRedPacketSend appRedPacketSend) {
        return appRedPacketSendMapper.updateAppRedPacketSend(appRedPacketSend);
    }

    /**
     * 批量删除红包发送记录
     *
     * @param ids 需要删除的红包发送记录主键
     * @return 结果
     */
    @Override
    public int deleteAppRedPacketSendByIds(Long[] ids) {
        return appRedPacketSendMapper.deleteAppRedPacketSendByIds(ids);
    }

    /**
     * 删除红包发送记录信息
     *
     * @param id 红包发送记录主键
     * @return 结果
     */
    @Override
    public int deleteAppRedPacketSendById(Long id) {
        return appRedPacketSendMapper.deleteAppRedPacketSendById(id);
    }

    @Autowired
    private IAppUserService userService;

    @Override
    public AjaxResult sendRedPacket(AppRedPacketSend redPacketSend) {
        AppUser userEntity = userService.getById(redPacketSend.getUserId());
        if (userEntity.getBalance().compareTo(redPacketSend.getMoney()) < 0) {
            return AjaxResult.error(-1, "用户余额不足");
        }

        // 用户余额内扣除红包金额
        userService.updateUserBalance(redPacketSend.getUserId(), redPacketSend.getMoney().negate());
        // 保存发送
        appRedPacketSendMapper.insertAppRedPacketSend(redPacketSend);
        Map<String, Object> map = new HashMap<>();
        map.put("redPacketId", redPacketSend.getId());
        return AjaxResult.ok(0, "发送成功", map);
    }

    @Override
    public AppRedPacketSend getById(Long redPacketId) {
        if (redPacketId != null) {
            return appRedPacketSendMapper.selectAppRedPacketSendById(redPacketId);
        }
        return null;
    }
}
